﻿Imports System.ComponentModel
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Form1
    Private nodes As New Dictionary(Of String, HarryNode)
    Public searchBgColor As Color = Color.LightGreen

    Public treeTargetPath As String
    Private projectSavePath As String
    'Private notUpdataNodesTree As Boolean

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Directory.Exists(WorkPathTextBox.Text) Then
            FBD.SelectedPath = WorkPathTextBox.Text
        End If

        If FBD.ShowDialog = DialogResult.OK Then
            WorkPathTextBox.Text = FBD.SelectedPath
        End If
    End Sub

    Private Sub WorkPathTextBox_TextChanged(sender As Object, e As EventArgs) Handles WorkPathTextBox.TextChanged
        If Directory.Exists(WorkPathTextBox.Text) Then
            LoadWorkPathJsonsName(WorkPathTextBox.Text)
        End If
    End Sub

    Private Sub LoadWorkPathJsonsName(workPath As String)
        Dim files = Directory.GetFiles(workPath)
        WorkPathListBox.Items.Clear()
        For Each file In files
            If Path.GetExtension(file) = ".json" Then
                WorkPathListBox.Items.Add(Path.GetFileNameWithoutExtension(file))
            End If
        Next
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'WorkJsonListBox.Items.Clear()

        For Each item In WorkPathListBox.SelectedItems
            Dim jsonPath = Path.Combine(WorkPathTextBox.Text, item & ".json")
            If Not WorkJsonListBox.Items.Contains(jsonPath) Then
                WorkJsonListBox.Items.Add(jsonPath)
            End If
        Next

        'If notUpdataNodesTree Then
        '    Return
        'End If
        If WorkJsonListBox.Items.Count Then
            UpdataTree()
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim removeItems(WorkJsonListBox.Items.Count - 1) As Object
        WorkJsonListBox.SelectedItems.CopyTo(removeItems, 0)
        For Each item In removeItems
            WorkJsonListBox.Items.Remove(item)
        Next

        If WorkJsonListBox.Items.Count Then
            UpdataTree(True)
        Else
            UpdataTree()
        End If
    End Sub

    Private Sub UpdataTree(Optional keepPath As Boolean = False)
        nodes.Clear()
        Dim notFoundPath As New List(Of String)
        For Each item In WorkJsonListBox.Items
            If File.Exists(item) Then
                nodes.Add(item, New HarryNode(Path.GetFileNameWithoutExtension(item), File.ReadAllText(item)))
            Else
                notFoundPath.Add(item)
            End If
        Next
        UpdataNodeTreeView(keepPath)
        NodesTreePathTextBox.BackColor = Color.Lime
        If notFoundPath.Count > 0 Then
            MsgBox("以下文件路径未找到" & vbCrLf & Join(notFoundPath.ToArray, vbCrLf), 16)
            For Each path In notFoundPath
                WorkJsonListBox.Items.Remove(path)
            Next
        End If
    End Sub
    Public Sub UpdataNodeTreeView(Optional keepPath As Boolean = False)

        Dim targetPath = "root\"
        If NodesTreeView.SelectedNode IsNot Nothing Then
            targetPath = NodesTreeView.SelectedNode.FullPath
        End If
        Dim 树展开情况 As New Dictionary(Of String, Boolean)
        Dim 树颜色情况 As New Dictionary(Of String, Color)
        For Each node In NodesTreeView.Nodes
            字典合并(树展开情况, 获得树展开情况(node))
            字典合并(树颜色情况, 获得树颜色情况(node))
        Next
        NodesTreeView.Nodes.Clear()
        For Each node In nodes
            Dim tree As TreeNode = node.Value.GetTreeNode(interpreter)
            NodesTreeView.Nodes.Add(tree)
            还原树展开(tree, 树展开情况)
            还原树颜色(tree, 树颜色情况)
        Next

        If keepPath Then
            ShowNodeTreeView(targetPath)
        End If

    End Sub
    Public Sub 还原树颜色(tree As TreeNode, d As Dictionary(Of String, Color))
        If d.ContainsKey(tree.FullPath) Then
            tree.BackColor = d(tree.FullPath)
        End If
        For Each node In tree.Nodes
            还原树颜色(node, d)
        Next
    End Sub
    Public Sub 还原树展开(tree As TreeNode, d As Dictionary(Of String, Boolean))
        If d.ContainsKey(tree.FullPath) Then
            tree.Expand()
            For Each node In tree.Nodes
                还原树展开(node, d)
            Next
        End If
    End Sub
    Public Function 获得树展开情况(tree As TreeNode) As Dictionary(Of String, Boolean)
        Dim result As New Dictionary(Of String, Boolean)
        If tree.IsExpanded Then
            result.Add(tree.FullPath, tree.IsExpanded)
            For Each node In tree.Nodes
                字典合并(result, 获得树展开情况(node))
            Next
        End If
        Return result
    End Function
    Public Function 获得树颜色情况(tree As TreeNode) As Dictionary(Of String, Color)
        Dim result As New Dictionary(Of String, Color)
        If tree.BackColor <> NodesTreeView.BackColor Then
            result.Add(tree.FullPath, tree.BackColor)
        End If
        For Each node In tree.Nodes
            字典合并(result, 获得树颜色情况(node))
        Next
        Return result
    End Function
    Public Sub 字典合并(d1 As Dictionary(Of String, Boolean), d2 As Dictionary(Of String, Boolean))
        For Each keyValue In d2
            If Not d1.ContainsKey(keyValue.Key) Then
                d1.Add(keyValue.Key, keyValue.Value)
            End If
        Next
    End Sub
    Public Sub 字典合并(d1 As Dictionary(Of String, Color), d2 As Dictionary(Of String, Color))
        For Each keyValue In d2
            If Not d1.ContainsKey(keyValue.Key) Then
                d1.Add(keyValue.Key, keyValue.Value)
            End If
        Next
    End Sub
    Private Function FindTreeNode(nodeName As String, nodes As TreeNodeCollection) As TreeNode
        For Each node As TreeNode In nodes
            If node.Text = nodeName Then
                Return node
            End If
        Next
        Return Nothing
    End Function
    Private Sub ShowNodeTreeView(path As String)
        Dim paths As String() = path.Split("\")
        Dim nowNode As TreeNode = FindTreeNode(paths(0), NodesTreeView.Nodes)
        Dim node As TreeNode
        If nowNode Is Nothing Then
            Return
        Else
            For i As Integer = 1 To paths.Length - 1
                node = FindTreeNode(paths(i), nowNode.Nodes)
                If node Is Nothing Then
                    If nowNode.Nodes.Count Then
                        nowNode = nowNode.Nodes(0)
                    End If
                    Exit For
                End If
                nowNode = node
            Next
        End If
        If nowNode IsNot Nothing Then
            NodesTreeView.SelectedNode = nowNode
        End If
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        HarryNode.pathSeparator = "\"
        If File.Exists("程序设置.json") Then
            Dim configNode As New HarryNode("configNode", File.ReadAllText("程序设置.json"))
            WorkPathTextBox.Text = configNode.GetAtt("工作目录")
            Dim changeJsons = configNode.GetAtt("修改JSON集")
            If changeJsons IsNot Nothing Then
                For Each node In changeJsons
                    WorkJsonListBox.Items.Add(node.value.value)
                Next
            End If
            If WorkJsonListBox.Items.Count Then
                UpdataTree()
                ShowNodeTreeView(configNode.GetAtt("选取路径", "root"))
            End If
            'Height = configNode.GetAtt("窗体大小\高", 774)
            'Width = configNode.GetAtt("窗体大小\宽", 1252)

            'WindowState = configNode.GetAtt("窗体状态", FormWindowState.Normal)
            'If WindowState = FormWindowState.Minimized Then
            '    WindowState = FormWindowState.Normal
            'End If
            'Dim centerLeft = (My.Computer.Screen.Bounds.Width - Width) / 2
            'Dim centerTop = (My.Computer.Screen.Bounds.Height - Height) / 2
            'Left = configNode.GetAtt("窗体位置\左", centerLeft)
            'Top = configNode.GetAtt("窗体位置\上", centerTop)
            'If Left < 0 Or Left > My.Computer.Screen.Bounds.Width Then
            '    Left = centerLeft
            'End If
            'If Top < 0 Or Top > My.Computer.Screen.Bounds.Height Then
            '    Top = centerTop
            'End If
            'Dim splitJsons = configNode.GetAtt("区域大小")
            'On Error Resume Next
            'If splitJsons IsNot Nothing Then
            '    For Each node In splitJsons
            '        Dim splitControl As SplitContainer = Controls.Find(node.key, True)(0)
            '        Dim SplitterDistance As Integer = node.value.GetAtt("SplitterDistance")
            '        splitControl.SplitterDistance = SplitterDistance
            '    Next
            'End If
        End If
        If File.Exists("主题.json") Then
            Dim configNode As New HarryNode("configNode", File.ReadAllText("主题.json"))
            Dim setBackColor As Color = Color.FromArgb(255, Color.FromArgb(configNode.GetAtt("背景颜色", BackColor.ToArgb)))
            Dim setForeColor As Color = Color.FromArgb(255, Color.FromArgb(configNode.GetAtt("字体颜色", ForeColor.ToArgb)))
            searchBgColor = Color.FromArgb(255, Color.FromArgb(configNode.GetAtt("搜索背景色", searchBgColor.ToArgb)))
            Dim fontName As String = configNode.GetAtt("字体", Font.Name)
            Dim fontSize As Single = configNode.GetAtt("字体大小", Font.Size)
            Dim setFont As New Font(fontName, fontSize)
            SetColorInChildControls(Me, setBackColor, setForeColor, setFont)
        End If
    End Sub
    Private Sub SetColorInChildControls(parentControl As Control, bC As Color, fC As Color, f As Font)
        For Each childControl As Control In parentControl.Controls
            childControl.BackColor = bC
            childControl.ForeColor = fC
            childControl.Font = f
            SetColorInChildControls(childControl, bC, fC, f)
        Next
    End Sub
    Private Function GetRootNode(node As TreeNode) As TreeNode
        If node.Parent IsNot Nothing Then
            Return GetRootNode(node.Parent)
        End If
        Return node
    End Function
    Private Function GetNodesId() As Integer
        'Dim pathList = NodesTreeView.SelectedNode.FullPath.Split("\")
        Dim rootNode As TreeNode = GetRootNode(NodesTreeView.SelectedNode)
        Return rootNode.Index
        'For i As Integer = 0 To nodes.Count - 1
        '    If Path.GetFileNameWithoutExtension(nodes.Keys(i)) = pathList(0) Then
        '        Return i
        '    End If
        'Next
        'Return -1
    End Function

    Private Sub NodesTreeView_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles NodesTreeView.AfterSelect
        Dim pathList = NodesTreeView.SelectedNode.FullPath.Split("\").ToList
        pathList.RemoveAt(0)
        Dim targetPath = GetTargetNodeTreePath() ' Join(pathList.ToArray, "\")
        ''If targetPath.StartsWith("\") Then
        ''    targetPath = targetPath.Substring(1)
        ''End If
        Dim nId As Integer = GetNodesId()
        If nId = -1 Then
            Return
        End If
        'If targetPath Is Nothing Then
        '    targetPath = ""
        'End If
        'targetPath = Regex.Replace(targetPath, "(.*?) \(.*\)", "$1")
        treeTargetPath = targetPath
        NodeValueTextBox.Text = nodes.Values(nId).GetNode(targetPath).ToString
        'notUpdataNodesTree = True
        WorkJsonListBox.SelectedItems.Clear()
        For itemId = 0 To WorkJsonListBox.Items.Count - 1
            Dim item = WorkJsonListBox.Items(itemId)
            If nodes.ContainsKey(item) Then
                If nodes(item).GetAtt(targetPath) IsNot Nothing Then
                    WorkJsonListBox.SelectedItems.Add(item)
                End If
            End If
        Next
        'notUpdataNodesTree = False
        NodesTreePathTextBox.Text = NodesTreeView.SelectedNode.FullPath
    End Sub

    Private Sub NodesTreeView_MouseUp(sender As Object, e As MouseEventArgs) Handles NodesTreeView.MouseUp

    End Sub

    Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        Dim configNode As New HarryNode("configNode", "{}")
        configNode.SetAtt("工作目录", WorkPathTextBox.Text, HarryNode.NodeTypeEnum.isString)
        Dim items As New List(Of String)
        Dim itemId As Integer = 0
        For Each item In WorkJsonListBox.Items
            configNode.SetAtt("修改JSON集\" & itemId, item, HarryNode.NodeTypeEnum.isString)
            itemId += 1
        Next
        If NodesTreeView.SelectedNode IsNot Nothing Then
            configNode.SetAtt("选取路径", NodesTreeView.SelectedNode.FullPath, HarryNode.NodeTypeEnum.isString)
        End If
        'configNode.SetAtt("区域大小", GetSplitContainerCofing())
        'configNode.SetAtt("窗体大小\宽", Width)
        'configNode.SetAtt("窗体大小\高", Height)
        'configNode.SetAtt("窗体状态", WindowState)
        File.WriteAllText("程序设置.json", configNode.ToJson)
    End Sub
    Private Function GetSplitContainerCofing() As String
        Dim node As New HarryNode("区域大小", "{}")
        Dim splitContainerControls As List(Of SplitContainer) = RecursionFindControl(Me, "SplitContainer")
        For Each splitContainerControl In splitContainerControls
            node.SetAtt(String.Format("{0}\SplitterDistance", splitContainerControl.Name), splitContainerControl.SplitterDistance, HarryNode.NodeTypeEnum.isSingle)
        Next
        Return node.ToString
    End Function
    Private Function RecursionFindControl(source As Control, typeName As String) As List(Of SplitContainer)
        Dim result As New List(Of SplitContainer)
        For Each sourceChild In source.Controls
            If sourceChild.GetType.Name = typeName Then
                result.Add(sourceChild)
            End If
            result.AddRange(RecursionFindControl(sourceChild, typeName))
        Next
        Return result
    End Function
    Private Function GetTargetNodeTreePath() As String
        Dim pathList = NodesTreeView.SelectedNode.FullPath.Split("\").ToList
        pathList.RemoveAt(0)
        Dim targetPath = Join(pathList.ToArray, "\")
        Dim nId As Integer = GetNodesId()
        If nId = -1 Then
            Return ""
        End If
        If targetPath Is Nothing Then
            targetPath = ""
        End If
        targetPath = Regex.Replace(targetPath, "(.*?) \(.*?\)(.*?)", "$1$2")
        If targetPath.StartsWith("\") Then
            targetPath = targetPath.Substring(1)
        End If
        Return targetPath
    End Function

    Private Sub 删除节点ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 删除节点ToolStripMenuItem.Click
        For Each node In nodes
            node.Value.Del(GetTargetNodeTreePath)
        Next
        NodesTreeView.Nodes.Remove(NodesTreeView.SelectedNode)
        NodesTreePathTextBox.BackColor = Color.Orange
        'UpdataNodeTreeView(True)
    End Sub

    Private Sub 保存修改ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 保存修改ToolStripMenuItem.Click
        For Each node In nodes
            File.WriteAllText(node.Key, node.Value.ToJson)
        Next
        NodesTreePathTextBox.BackColor = Color.Lime
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        NodesSetValue(NodeValueTextBox.Text)
    End Sub
    Private Sub NodesSetValue(value As String)
        For Each node In nodes
            node.Value.SetAtt(GetTargetNodeTreePath(), value)
        Next
        NodesTreePathTextBox.BackColor = Color.Orange
        UpdataNodeTreeView(True)
    End Sub

    Private Sub 复制键名ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 复制键名ToolStripMenuItem.Click
        Dim nId As Integer = GetNodesId()
        If nId = -1 Then
            Return
        End If
        Dim nodeJson As String = nodes.Values(nId).GetNode(GetTargetNodeTreePath).nodeName
        If nodeJson IsNot Nothing Then
            Clipboard.Clear()
            Clipboard.SetText(nodeJson)
        End If
    End Sub

    Private Sub WorkJsonListBox_DragDrop(sender As Object, e As DragEventArgs) Handles WorkJsonListBox.DragDrop
        For Each dropPath As String In e.Data.GetData(DataFormats.FileDrop)
            If Not WorkJsonListBox.Items.Contains(dropPath) Then
                WorkJsonListBox.Items.Add(dropPath)
            End If
        Next
        UpdataTree(True)
    End Sub

    Private Sub WorkJsonListBox_DragEnter(sender As Object, e As DragEventArgs) Handles WorkJsonListBox.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.All
        End If
    End Sub

    Private Sub 重载节点ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 重载节点ToolStripMenuItem.Click
        UpdataTree(True)
    End Sub

    Private Sub GroupBox3_Enter(sender As Object, e As EventArgs) Handles GroupBox3.Enter

    End Sub

    Private Sub 复制节点ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 复制节点ToolStripMenuItem.Click
        Dim nId As Integer = GetNodesId()
        If nId = -1 Then
            Return
        End If
        Dim nodeJson As String = nodes.Values(nId).GetNode(GetTargetNodeTreePath).ToJson
        If nodeJson IsNot Nothing Then
            Clipboard.Clear()
            Clipboard.SetText(nodeJson)
        End If
    End Sub

    Private Sub Json节点菜单_Opening(sender As Object, e As CancelEventArgs) Handles Json节点菜单.Opening
        Dim clipData As String = Clipboard.GetText
        粘贴节点ToolStripMenuItem.Enabled = clipData IsNot Nothing AndAlso clipData.Length > 0
        If 粘贴节点ToolStripMenuItem.Enabled Then
            粘贴节点ToolStripMenuItem.ToolTipText = clipData
        End If
    End Sub

    Private Sub 粘贴节点ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 粘贴节点ToolStripMenuItem.Click
        Dim clipData As String = Clipboard.GetText
        If clipData IsNot Nothing AndAlso clipData.Length > 0 Then
            NodesSetValue(clipData)
            UpdataNodeTreeView(True)
        End If
    End Sub

    Private Sub 同步节点ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 同步节点ToolStripMenuItem.Click
        Dim nId As Integer = GetNodesId()
        If nId = -1 Then
            Return
        End If
        Dim treePath = GetTargetNodeTreePath()
        Dim syncNode As HarryNode = nodes.Values(nId).GetNode(treePath)
        If syncNode IsNot Nothing Then
            For Each node In nodes.Values
                node.AddNode(treePath, syncNode.nodeName, syncNode.ToJson)
            Next
        End If
        NodesTreePathTextBox.BackColor = Color.Orange
        UpdataNodeTreeView(True)
    End Sub

    Private Sub 添加节点ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 添加节点ToolStripMenuItem.Click
        Dim clipData As String = Clipboard.GetText
        If clipData IsNot Nothing AndAlso clipData.Length > 0 Then
            Form2.TextBox1.Text = ""
            Form2.TextBox2.Text = clipData
        End If
        Form2.treePath = treeTargetPath
        If Form2.ShowDialog = DialogResult.OK Then
            NodesTreePathTextBox.BackColor = Color.Orange
            UpdataNodeTreeView(True)
        End If
    End Sub
    Public Sub 新增节点(treePath As String, nodeName As String, nodeJson As String)
        treePath &= "\" & nodeName
        If treePath.StartsWith("\") Then
            treePath = treePath.Substring(1)
        End If
        For Each node In nodes.Values
            node.AddNode(treePath, nodeName, nodeJson)
        Next
        NodesTreePathTextBox.BackColor = Color.Orange
    End Sub

    Private Sub 搜索ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 搜索ToolStripMenuItem.Click
        SearchForm.Show()
    End Sub

    Public Sub Search(content As String, regMod As Boolean, Optional nodes As TreeNodeCollection = Nothing)
        If nodes Is Nothing Then
            nodes = NodesTreeView.Nodes
        End If
        For Each node As TreeNode In nodes
            Dim isFindNode As Boolean
            If regMod Then
                isFindNode = Regex.IsMatch(node.Text, content, RegexOptions.Singleline)
            Else
                isFindNode = node.Text.IndexOf(content) >= 0
            End If
            If isFindNode Then
                node.BackColor = searchBgColor
                node.EnsureVisible()
            Else
                If node.BackColor <> NodesTreeView.BackColor Then
                    node.BackColor = NodesTreeView.BackColor
                End If
                'If node.IsExpanded Then
                '    node.Collapse()
                'End If
            End If
            Search(content, regMod, node.Nodes)
        Next
    End Sub
    Private Sub SetSingleNodeValue(path As String, value As String)
        Dim nid As Integer = GetNodesId()
        Dim node As HarryNode = nodes.Values(nid)
        node.SetAtt(path, value)
        UpdataNodeTreeView(True)
        NodesTreePathTextBox.BackColor = Color.Orange
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Dim treePath = GetTargetNodeTreePath()
        SetSingleNodeValue(treePath, NodeValueTextBox.Text)
    End Sub

    Private Sub WorkPathListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles WorkPathListBox.SelectedIndexChanged

    End Sub

    Private Sub WorkPathListBox_DragEnter(sender As Object, e As DragEventArgs) Handles WorkPathListBox.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.All
        End If
    End Sub

    Private Sub WorkPathListBox_DragDrop(sender As Object, e As DragEventArgs) Handles WorkPathListBox.DragDrop
        Dim loadPath As String = e.Data.GetData(DataFormats.FileDrop)(0)
        If Directory.Exists(loadPath) Then
            WorkPathTextBox.Text = loadPath
        Else
            WorkPathTextBox.Text = Path.GetDirectoryName(e.Data.GetData(DataFormats.FileDrop)(0))
        End If
    End Sub

    Private Sub 改节点名ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 改节点名ToolStripMenuItem.Click
        Dim nId As Integer = GetNodesId()
        If nId = -1 Then
            Return
        End If
        Dim targetPath = GetTargetNodeTreePath()
        treeTargetPath = targetPath
        Dim targetNode As HarryNode = nodes.Values(nId).GetNode(targetPath)
        Dim newName As String = InputBox("输入新节点名：", "节点名修改", targetNode.nodeName)
        If newName <> "" And newName <> targetNode.nodeName Then
            nodes.Values(nId).ReName(targetPath, newName)
            UpdataNodeTreeView(True)
            NodesTreePathTextBox.BackColor = Color.Orange
        End If
    End Sub

    Private Sub 白名单过滤ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 白名单过滤ToolStripMenuItem.Click
        Dim filterStr As String = InputBox("输入过滤关键词，并用英文逗号分隔：", "白名单过滤", Clipboard.GetText())
        If filterStr <> "" Then
            Dim filters() As String = filterStr.Split(",")
            Dim row As Integer = 0
            While row < WorkPathListBox.Items.Count
                If InFilters(WorkPathListBox.Items(row), filters) Then
                    row += 1
                Else
                    WorkPathListBox.Items.RemoveAt(row)
                End If
            End While
        End If
    End Sub

    Private Function InFilters(checkStr As String, filters() As String) As Boolean
        For Each filter As String In filters
            If checkStr Like "*" & filter & "*" Then
                Return True
            End If
        Next
        Return False
    End Function

    Private Sub 重载目录ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 重载目录ToolStripMenuItem.Click
        If Directory.Exists(WorkPathTextBox.Text) Then
            LoadWorkPathJsonsName(WorkPathTextBox.Text)
        End If
    End Sub

    Private Sub 全选ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 全选ToolStripMenuItem.Click
        AllSelect(WorkJsonListBox)
    End Sub

    Private Sub AllSelect(targetListBox As ListBox)
        For index As Integer = 0 To targetListBox.Items.Count - 1
            targetListBox.SetSelected(index, True)
        Next
    End Sub

    Private Sub 删除ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 删除ToolStripMenuItem.Click
        Button3_Click(Nothing, Nothing)
    End Sub

    Private Sub 全选ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles 全选ToolStripMenuItem1.Click
        AllSelect(WorkPathListBox)
    End Sub

    Private Sub ToolStripMenuItem3_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem3.Click
        Button2_Click(Nothing, Nothing)
    End Sub

    Private Sub ToolStripMenuItem4_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem4.Click
        Dim nId As Integer = GetNodesId()
        If nId = -1 Then
            Return
        End If
        Dim targetNode As HarryNode = nodes.Values(nId).GetNode(GetTargetNodeTreePath)
        Dim nodeJson As String = targetNode.ToJson
        If nodeJson IsNot Nothing Then
            Clipboard.Clear()
            Clipboard.SetText(String.Format("{{""{0}"":{1}}}", targetNode.nodeName, nodeJson))
        End If
    End Sub

    Private Sub ToolStripMenuItem5_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem5.Click
        Dim clipData As String = Clipboard.GetText
        If clipData IsNot Nothing AndAlso clipData.Length > 0 Then
            Dim clipNode As New HarryNode("clip", clipData)
            clipNode = clipNode.GetNode(clipNode.GetChildPath()(0))
            新增节点(GetTargetNodeTreePath, clipNode.nodeName, clipNode.ToJson)
            UpdataNodeTreeView(True)
        End If
    End Sub

    Private Sub 另存为项目ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 另存为项目ToolStripMenuItem.Click
        If SFD.ShowDialog = DialogResult.OK Then
            Dim saveString As String = GetProjectData()
            File.WriteAllText(SFD.FileName, saveString)
            projectSavePath = SFD.FileName
        End If
    End Sub

    Private Function GetProjectData() As String
        Dim result As New List(Of String)
        For Each path In WorkJsonListBox.Items
            result.Add(path)
        Next
        Return Join(result.ToArray, vbCrLf)
    End Function

    Private Sub 保存项目ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 保存项目ToolStripMenuItem.Click
        If File.Exists(projectSavePath) Then
            Dim saveString As String = GetProjectData()
            File.WriteAllText(projectSavePath, saveString)
        Else
            另存为项目ToolStripMenuItem_Click(Nothing, Nothing)
        End If
    End Sub
    Private Sub SetProjectData(path As String, Optional add As Boolean = False)
        projectSavePath = path
        Dim projectData() As String = Split(File.ReadAllText(path), vbCrLf)
        If Not add Then
            WorkJsonListBox.Items.Clear()
        End If
        For Each p As String In projectData
            If Not WorkJsonListBox.Items.Contains(p) Then
                WorkJsonListBox.Items.Add(p)
            End If
        Next
        If WorkJsonListBox.Items.Count Then
            UpdataTree(True)
        Else
            UpdataTree()
        End If
    End Sub

    Private Sub 读取项目ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 读取项目ToolStripMenuItem.Click
        If OFD.ShowDialog = DialogResult.OK Then
            SetProjectData(OFD.FileName)
        End If
    End Sub

    Private Sub ToolStripMenuItem6_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem6.Click
        Dim addCount As Integer = Val(InputBox("输入增殖数量：", "增殖节点", 1))
        If addCount <= 0 Then
            Exit Sub
        End If
        Dim nId As Integer = GetNodesId()
        If nId = -1 Then
            Return
        End If
        Dim targetNode As HarryNode = nodes.Values(nId).GetNode(GetTargetNodeTreePath)
        For i As Integer = 1 To addCount
            targetNode.parentNode.AddNode(targetNode.nodeName & i, targetNode.nodeName & i, targetNode.ToJson)
        Next
        NodesTreePathTextBox.BackColor = Color.Orange
        UpdataNodeTreeView(True)
    End Sub

    Private Sub WorkJsonListBox_Format(sender As Object, e As ListControlConvertEventArgs) Handles WorkJsonListBox.Format
        GroupBox2.Text = String.Format("修改JSON集({0})-{1}", WorkJsonListBox.Items.Count, Path.GetFileNameWithoutExtension(projectSavePath))
    End Sub

    Private Sub ToolStripMenuItem9_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem9.Click
        For i As Integer = 0 To WorkJsonListBox.Items.Count - 1
            WorkJsonListBox.SetSelected(i, Not WorkJsonListBox.GetSelected(i))
        Next
    End Sub

    Private Sub 隐藏子树ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 隐藏子树ToolStripMenuItem.Click
        Dim hidePath() As String = NodesTreeView.SelectedNode.FullPath.Split("\")
        If hidePath.Length = 1 Then
            For Each node As TreeNode In NodesTreeView.Nodes
                node.Collapse()
            Next
            Exit Sub
        End If
        For Each node In NodesTreeView.Nodes
            隐藏同级节点(node.nodes, hidePath, 1, hidePath.Length - 1)
        Next
    End Sub
    Private Sub 隐藏同级节点(nodes As TreeNodeCollection, hidePath() As String, deep As Integer, maxDeep As Integer)
        For Each node As TreeNode In nodes
            If deep < maxDeep Then
                If node.Text = hidePath(deep) Then
                    隐藏同级节点(node.Nodes, hidePath, deep + 1, maxDeep)
                End If
            ElseIf node.Text = hidePath(deep) Then
                node.Collapse()
            End If
        Next
    End Sub

    Private Sub 用默认应用打开ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 用默认应用打开ToolStripMenuItem.Click
        For Each item In WorkJsonListBox.SelectedItems
            If File.Exists(item) Then
                Process.Start(item)
            End If
        Next
    End Sub

    Private Sub ToolStripMenuItem12_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem12.Click
        If OFD.ShowDialog = DialogResult.OK Then
            SetProjectData(OFD.FileName, True)
        End If
    End Sub

    Private Sub ToolStripMenuItem13_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem13.Click
        'Form3.nodeRootPath = GetTargetNodeTreePath()
        'Form3.Show()

    End Sub
    Private Sub OpenCanZhao(sender As Object, e As EventArgs)
        Form3.nodeRootPath = GetTargetNodeTreePath()
        Form3.setName = sender.text
        Form3.Show()
    End Sub

    Private Sub ToolStripMenuItem13_MouseEnter(sender As Object, e As EventArgs) Handles ToolStripMenuItem13.MouseEnter
        ToolStripMenuItem13.DropDownItems.Clear()
        Dim setNames = 参照.GetSets()
        For Each setName In setNames
            ToolStripMenuItem13.DropDownItems.Add(setName, Nothing, AddressOf OpenCanZhao)
        Next
    End Sub

    Private Sub ToolStripMenuItem14_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem14.Click
        If File.Exists(WorkJsonListBox.SelectedItem) Then
            If SFDJSON.ShowDialog() Then
                File.WriteAllText(SFDJSON.FileName, File.ReadAllText(WorkJsonListBox.SelectedItem))
            End If
        End If
    End Sub

    Private Sub 打开文件位置ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 打开文件位置ToolStripMenuItem.Click
        If File.Exists(WorkJsonListBox.SelectedItem) Then
            Process.Start("explorer.exe", "/select," & WorkJsonListBox.SelectedItem)
        End If
    End Sub

    Private Sub WorkPathListBox_DoubleClick(sender As Object, e As EventArgs) Handles WorkPathListBox.DoubleClick
        Button2_Click(Nothing, Nothing)
    End Sub
End Class
